package com.flymobi.service;

import java.util.ArrayList;
import java.util.List;

import com.flymobi.fw.modules.CommonService;
import com.flymobi.po.Zhuanti;
import com.flymobi.util.QL;
import com.flymobi.vo.ZhuantiSearchVo;

public class ZhuantiService extends CommonService {

	public List<Zhuanti> list() {
		return dao.find(Zhuanti.class, QL.HQL.Zhuanti.LIST, new Object[] {});
	}
	
	public List<Zhuanti> list(ZhuantiSearchVo param,int startRow, int pagePerSize) {
		String hql="from Zhuanti m where 1=1 ";
		List<Object> paramList=new ArrayList<Object>();
		if(param!=null){
			if(param.getStatus()!=null&&param.getStatus()!=-1){
				hql+=" and m.status =?";
				paramList.add(param.getStatus());
			}
			if(param.getEnd()!=null){
				hql+=" and m.savetime<?";
				paramList.add(param.getEnd());
			}
			if(param.getStart()!=null){
				hql+=" and m.savetime>?";
				paramList.add(param.getStart());
			}
			if(param.getType()!=null&&!param.getType().equals("-1")){
				hql+=" and m.type=?";
				paramList.add(param.getType());
			}
		}
		hql+=" order by m.savetime desc";
		return dao.find(Zhuanti.class,hql, paramList,startRow, pagePerSize);
	}
	
	public Long getCount(ZhuantiSearchVo param) {
		String hql="select count(*) from Zhuanti m where 1=1 ";
		List<Object> paramList=new ArrayList<Object>();
		if(param!=null){
			if(param.getStatus()!=null&&param.getStatus()!=-1){
				hql+=" and m.status =?";
				paramList.add(param.getStatus());
			}
			if(param.getEnd()!=null){
				hql+=" and m.savetime<?";
				paramList.add(param.getEnd());
			}
			if(param.getStart()!=null){
				hql+=" and m.savetime>?";
				paramList.add(param.getStart());
			}
			if(param.getType()!=null&&!param.getType().equals("-1")){
				hql+=" and m.type=?";
				paramList.add(param.getType());
			}
		}
		return dao.getCount(hql, paramList);
	}
	
	public List<Zhuanti> list(Long fatherid) {
		return dao.find(Zhuanti.class, QL.HQL.Zhuanti.LIST_SUB, new Object[] {fatherid});
	}


	public void save(Zhuanti Zhuanti) {
		dao.save(Zhuanti);
	}

	public Zhuanti load(String id) {
		return dao.load(Zhuanti.class, new Long(id));
	}
	
	public Zhuanti get(String id) {
		return dao.get(Zhuanti.class, new Long(id));
	}

	public void delete(String id) {
		dao.delete(this.get(id));
	}
	
	public void updateUp(String id) {
		Zhuanti pe = this.get(id);
		Zhuanti peUp = findUp(pe);
		if (peUp != null) {
			switchPosion(pe, peUp);
		}
	}
	
	public void updateDown(String id) {
		Zhuanti pe = this.get(id);
		Zhuanti peUp = findDown(pe);
		if (peUp != null) {
			switchPosion(pe, peUp);
		}
	}

	private void switchPosion(Zhuanti pe, Zhuanti peUp) {
		long pe1idx = pe.getIdx();
		pe.setIdx(peUp.getIdx());
		peUp.setIdx(pe1idx);
		dao.save(pe);
		dao.save(peUp);
	}

	private Zhuanti findUp(Zhuanti pe) {
		return dao.findSingle(Zhuanti.class, QL.HQL.Zhuanti.LIST_UP, new Object[] {
			pe.getFatherId(), pe.getIdx() });
	}
	
	private Zhuanti findDown(Zhuanti pe) {
		return dao.findSingle(Zhuanti.class, QL.HQL.Zhuanti.LIST_DOWN, new Object[] {
			pe.getFatherId(), pe.getIdx() });
	}
	
	public Long getSeq(Long fatherid){
		return dao.getSeq(QL.HQL.Zhuanti.GET_MAX_IDX, new Object[]{fatherid});
	}
}
